import java.lang.invoke.MethodHandle;
import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;

/**
 * Created by xueal on 2018/1/15.
 */
public class PlayerManager {

    private class PlayerInfo{
        private String name;
        private String pwd;

        public PlayerInfo(String name, String pwd) {
            this.name = name;
            this.pwd = pwd;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPwd() {
            return pwd;
        }

        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    }

    private HashMap<String, PlayerInfo> mPlayerMap;

    private static final PlayerManager single = new PlayerManager();

    private PlayerManager() {
        mPlayerMap = new HashMap<String, PlayerInfo>();
    }

    //静态工厂方法
    public static PlayerManager getInstance() {
        return single;
    }

    public String addPlayer(String name, String pwd) {

        PlayerInfo playerInfo = new PlayerInfo(name, pwd);
        String playerUuid = UUID.randomUUID().toString();

        mPlayerMap.put(playerUuid, playerInfo);

        return playerUuid;
    }

    public PlayerInfo getPlayer(String id){
        return mPlayerMap.get(id);
    }

    public boolean isPlayerExist(String name){
        if (name == null) return false;

        Iterator keys = mPlayerMap.keySet().iterator();
        while(keys.hasNext()){
            String key = (String)keys.next();
            if(name.equals(key)){
                return true;
            }
        }
        return false;
    }
}
